package com.example.chenjun.myapplication.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.chenjun.myapplication.R;
import com.example.chenjun.myapplication.bean.CookBean;
import com.example.chenjun.myapplication.mvp.MvpActivity;
import com.example.chenjun.myapplication.util.DensityUtils;
import com.example.chenjun.myapplication.widget.AutoNewLineLayout;

public class CookDetailActivity extends MvpActivity {

    private TextView mTvTitle;
    private TextView mTvContent;
    private LinearLayout mLlIvContainer, mLLContent;
    private AutoNewLineLayout mFuncContainer, mResContainer, mAssetContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_activity_cook_detail);
        mTvTitle = findViewById(R.id.tv_title);
        mTvContent = findViewById(R.id.tv_content);
        mLlIvContainer = findViewById(R.id.ll_iv_container);
        mLLContent = findViewById(R.id.ll_content);
        mFuncContainer = findViewById(R.id.container_func);
        mResContainer = findViewById(R.id.container_res);
        mAssetContainer = findViewById(R.id.container_asset);
        CookBean bean = (CookBean) getIntent().getSerializableExtra("bean");
        mTvTitle.setText(bean.getTitle());
        mTvContent.setText(bean.getImtro());
        String[] funcs = bean.getTags().split(";");
        for (String str : bean.getAlbums()) {
            mLlIvContainer.addView(createImageView(str));
        }
        for (String str : funcs) {
            mFuncContainer.addView(createTipsTextView(str, Color.parseColor("#67a04f"), R.drawable.app_shape_tv_tips));
        }
        String[] reses = bean.getIngredients().split(";");
        for (String str : reses) {
            mResContainer.addView(createTipsTextView(str, Color.parseColor("#3a60c5"), R.drawable.app_shape_tv_res));
        }
        String[] assets = bean.getBurden().split(";");
        for (String str : assets) {
            mAssetContainer.addView(createTipsTextView(str, Color.parseColor("#e41a3f"), R.drawable.app_shape_tv_asset));
        }
        for (CookBean.StepsBean step : bean.getSteps()) {
            mLLContent.addView(createContentTextView(step.getStep()));
            mLLContent.addView(createImageView(step.getImg()));
        }
    }

    private ImageView createImageView(String url){
        ImageView iv = new ImageView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtils.dp2px(this, 150));
        lp.setMargins(DensityUtils.dp2px(this, 40), 0, DensityUtils.dp2px(this, 40), DensityUtils.dp2px(this, 5));
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setLayoutParams(lp);
        Glide.with(this)
                .asBitmap()
                .load(url)
                .into(iv);
        return iv;
    }

    private TextView createContentTextView(String text){
        TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.app_layout_tv_step, mLLContent, false);
        tv.setText(text);
        return tv;
    }

    private TextView createTipsTextView(String text, int color, int drawableId){
        TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.app_layout_tv_tips, mFuncContainer, false);
        tv.setBackgroundResource(drawableId);
        int start = text.indexOf(",");
        if (start == -1){
            tv.setText(text);
            return tv;
        }
        text = text.replace(",", " ");
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        builder.setSpan(new ForegroundColorSpan(color), start + 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(builder);
        return tv;
    }
}
